package com.cyou.gamehistory.admin.web;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cyou.framework.pagination.proxy.PaginationSupport;
import com.cyou.framework.util.StringUtility;
import com.cyou.gamehistory.entity.AppInfo;
import com.cyou.gamehistory.entity.GameHistory;
import com.cyou.gamehistory.entity.UserInfo;
import com.cyou.gamehistory.entity.vo.AppStat;
import com.cyou.gamehistory.entity.vo.GameStat;
import com.cyou.gamehistory.service.AppInfoService;
import com.cyou.gamehistory.service.GameHistoryFeedService;
import com.cyou.gamehistory.service.GameHistoryService;
import com.cyou.gamehistory.service.GameService;
import com.cyou.gamehistory.service.UserInfoService;


/***
 * 后台游戏编年史
 * @author wangzhang
 * date 2012-12-24 下午1:45:23
 */
@Controller
@RequestMapping(value = "/a/gamehistory")
public class GamehistoryController {
	@Autowired
	private GameHistoryService gameHistoryService;
	@Autowired
	private AppInfoService appInfoService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private GameHistoryFeedService gameHistoryFeedService;
	@Autowired
	private GameService gameService;
	@RequestMapping("list")
	public String list(Model model,String order,String createTimeBegin,String createTimeEnd,String story,Integer pageNo,String flag) throws UnsupportedEncodingException{
		if(order==null){
			order="asc";
		}
		if(flag!=null){
			if(StringUtility.isNotBlank(story)){
				story=new String(story.getBytes("iso8859-1"), "UTF-8");
			}
		}else{
			story=story==null?null:story.trim();
		}
		pageNo = (pageNo == null || pageNo == 0 ? 1 : pageNo);
		PaginationSupport<GameHistory> gameHistoryList=gameHistoryService.getAllGameHistoryList(order, createTimeBegin, createTimeEnd, story, 15, pageNo);
		model.addAttribute("gameHistoryListPage", gameHistoryList);
		model.addAttribute("orderFlag", order);
		model.addAttribute("createTimeBegin", createTimeBegin);
		model.addAttribute("createTimeEnd", createTimeEnd);
		model.addAttribute("story", story);
		
		return "admin/story_list";
	}
	
	@RequestMapping("stat")
	public String stat(Model model){
		//高频次游戏排名
		List<GameStat> listGame=gameHistoryService.getTopGames();
		//高频次年份排名
		List<GameHistory> listYear=gameHistoryService.getTopYears();
		//总次数
		Integer totalCount=gameHistoryService.getTotal();
		Integer totalGameCount=gameService.getTotalCount();
		//应用使用人数与使用次数
		AppInfo appInfo=appInfoService.getAppInfo();
		model.addAttribute("appInfo", appInfo);
		model.addAttribute("gameStat", listGame);
		model.addAttribute("yearStat", listYear);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("totalGameCount", totalGameCount);
		 
		Date date=new Date();
		GregorianCalendar gc =new GregorianCalendar();
		gc.setTime(date);
		gc.add(GregorianCalendar.MONTH, -2);
		gc.getTime();
		String from=new SimpleDateFormat("yyyy-MM-dd").format(gc.getTime());
		gc.setTime(date);
		gc.add(GregorianCalendar.MONTH, -1);
		String to=new SimpleDateFormat("yyyy-MM-dd").format(gc.getTime());
		model.addAttribute("from", from);
		model.addAttribute("to", to);
		
		
		return "admin/data_stat";
	}
	
	@RequestMapping("delete")
	public String delete(Model model,String[] deleteHistoryId) throws UnsupportedEncodingException{
		if(deleteHistoryId!=null&&deleteHistoryId.length>0){
			gameHistoryService.deleteHistory(deleteHistoryId);
		}
		return this.list(model, null, null, null, null, null,null);
	}
	
	@RequestMapping("userlist")
	public String userlist(Model model,String order,Integer pageNo,String createTimeBegin,String createTimeEnd,String nickname,String flag) throws UnsupportedEncodingException{
		pageNo = (pageNo == null || pageNo == 0 ? 1 : pageNo);
		if(order==null){
			order="createtimeasc";
		}
		if(flag!=null){
			if(StringUtility.isNotBlank(nickname)){
				nickname=new String(nickname.getBytes("iso8859-1"), "UTF-8");
			}
		}else{
			nickname=nickname==null?null:nickname.trim();
		}
		PaginationSupport<UserInfo> userInfoList=userInfoService.getUserInfoList(nickname,order,createTimeBegin,createTimeEnd, 10, pageNo);
		model.addAttribute("userInfoListPage", userInfoList);
		model.addAttribute("orderFlag", order);
		model.addAttribute("createTimeBegin", createTimeBegin);
		model.addAttribute("createTimeEnd", createTimeEnd);
		model.addAttribute("nickname", nickname);
		return "admin/user_list";
	}
	@RequestMapping("appStat")
	@ResponseBody
	public List<AppStat> appStat(String from,String to) throws ParseException{
		List<AppStat> list=gameHistoryFeedService.getAppStat(from, to);
		return list;
	}
	
}
